package com.geralt.im.gui;

import geralt.im.watcher.Observer;

import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.geralt.im.entrance.Entrance;
import com.geralt.im.model.Customer;
import com.geralt.im.model.Message;
import com.geralt.im.protocol.Request;

public class RegisterGUI extends JFrame implements Observer {

	// private JFrame frame;
	private JTextField nametxt;
	private JButton cancel;
	private JButton register;
	private JPasswordField passwordField;
	private JPasswordField confirmpwdField;
	private JPanel infoPanel;
	private JPanel headPanel;
	private JLabel loginLabel;
	private JPanel btnPanel;
	/**
	 * Launch the application.
	 */

	/**
	 * Create the application.
	 */
	public RegisterGUI() {
		initialize();
	}

	/**
	 * Initialize the contents of the
	 */
	private void initialize() {
		// frame = new JFrame();
		setBounds(100, 200, 400, 595);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		getContentPane().setLayout(null);
		setLocationRelativeTo(null);
		setResizable(false);

		infoPanel = new JPanel();
		infoPanel.setBounds(22, 117, 394, 300);
		getContentPane().add(infoPanel);
		infoPanel.setLayout(null);

		confirmpwdField = new PasswordWithHint("enter your password again.");
		confirmpwdField.setBounds(82, 228, 203, 35);
		infoPanel.add(confirmpwdField);

		JLabel confirmpwd = new JLabel("confirm password");
		confirmpwd.setBounds(82, 194, 203, 23);
		infoPanel.add(confirmpwd);

		passwordField = new PasswordWithHint("length should less than 16.");
		passwordField.setBounds(82, 148, 203, 35);
		infoPanel.add(passwordField);

		JLabel password = new JLabel("password");
		password.setBounds(82, 114, 203, 23);
		infoPanel.add(password);

		nametxt = new TextFieldWithHint("name's length should less than 20.");
		nametxt.setBounds(82, 68, 203, 35);
		infoPanel.add(nametxt);
		// nametxt.setText("323232");
		nametxt.setColumns(10);

		JLabel username = new JLabel("username");
		username.setBounds(82, 34, 203, 23);
		infoPanel.add(username);

		headPanel = new ImagePanel("headImg.gif");
		headPanel.setBounds(0, -94, 394, 208);
		headPanel.setBackground(Color.white);
		getContentPane().add(headPanel);
		headPanel.setLayout(null);

		loginLabel = new JLabel("REGISTER");
		loginLabel.setBounds(87, 114, 212, 72);
		loginLabel.setFont(new java.awt.Font("Dialog", 1, 24));
		loginLabel.setForeground(Color.black);
		loginLabel.setHorizontalAlignment(JLabel.CENTER);
		headPanel.add(loginLabel);
		btnPanel = new JPanel();
		btnPanel.setBounds(22, 408, 394, 146);
		getContentPane().add(btnPanel);
		btnPanel.setLayout(null);

		register = new JButton("sign up");
		register.setBounds(115, 39, 129, 23);
		btnPanel.add(register);

		cancel = new JButton("cancel");
		cancel.setBounds(115, 73, 129, 23);
		btnPanel.add(cancel);
		cancel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				dispose();
			}
		});
		register.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				String name = nametxt.getText();
				String regex = "[A-Za-z0-9_]{1,20}";
				Pattern p = Pattern.compile(regex);
				Matcher m = p.matcher(name);
				String pwd = new String(passwordField.getPassword());
				String pwd2 = new String(confirmpwdField.getPassword());
				if (name == null || !m.matches()) {
					JOptionPane.showMessageDialog(null,
							"the username you have entered is invalid",
							"username invalid", JOptionPane.ERROR_MESSAGE);
				} else if (!pwd.equals(pwd2)) {
					JOptionPane.showMessageDialog(null,
							"two passwords you have entered is not match!",
							"password not match error",
							JOptionPane.ERROR_MESSAGE);
				} else if(pwd.length()<3){
					JOptionPane.showMessageDialog(null,
							"Your password is too weak, please longer than 3!",
							"weak password error",
							JOptionPane.ERROR_MESSAGE);
				} 
				
				else {
					Request request = new Request();
					request.setType("REGISTER");
					Entrance.customer = new Customer();
					Entrance.customer.setId(name);
					Entrance.customer.setPassword(pwd);
					request.getParameter().put("REGISTER", Entrance.customer);
					Entrance.client.getClientRunner().write(request);
					try {
						Entrance.client.getClientRunner().read();
					} catch (Exception e1) {
						// JOptionPane.showMessageDialog(null,"Can't connect to Server");
					}
				}
			}
		});

	}

	@Override
	public void update(int type, Object object) {
		// if (Entrance.onLogin == true)
		// return;
		
		switch (type) {
		case 1: // BadRequestCommand
			if("shutdown".equals(((Message) object).getContent())){
				JOptionPane.showMessageDialog(this,
						"Server shutdown,Program shutdown", "Error",
						JOptionPane.ERROR_MESSAGE);
				System.exit(1);
			}
		case 5:
			setVisible(false);
			// IParser parser = RpParser.getInstance();
			break;
		default:
			break;
		}
	}
}
